function setup(obj) {
	var egName=obj.value;
	if(obj.checked) {
		createIMG(egName);
	} else {
		var child=document.getElementById(egName);
		engineslist.removeChild(child);
	}
	preview();
}

function preview() {
	var nodes=engineslist.childNodes;
	var newEgs=new Array();
	for(var i=0; i<nodes.length-1; ++i) {
		newEgs.push(nodes[i].id);
	}
	safari.self.tab.dispatchMessage("preview", newEgs);	
}


function zoomChange(value) {
	if(value>0) {
		zoomLevel=value;
	} else {
		zoomLevel="1.0";
		zoom.value=zoomLevel;
	}
	safari.self.tab.dispatchMessage("zoom_change", zoomLevel);	
}

function tabPosChange(value) {
	safari.self.tab.dispatchMessage("tabPos_change", value);
}

function activeTabChange(value) {
	safari.self.tab.dispatchMessage("activeTab_change", value);
}

var srcId='';
function createIMG(egName) {
	var child=document.createElement("img");
	child.src='img/'+egName+'.png';
	child.className="eicon";
	child.id=egName;
	child.style.width="48px";
	child.style.height="48px";
	child.draggable=true;

	child.ondragstart=function(e) {
		srcId = this.id;
	}
	child.ondragenter=function(e) {
		//	var srcId=e.dataTransfer.getData('Text');
		var source=document.getElementById(srcId);
		if(this.id==srcId) {
			return;
		}
		engineslist.insertBefore(source,this);
	}
	child.ondragover=function(e) {
		e.preventDefault();
	}
	child.ondragend=preview;
	engineslist.insertBefore(child, engineslist.lastChild);
}

function foolast(target, e) {
//	var srcId=e.dataTransfer.getData('Text');
	var source=document.getElementById(srcId);
	target.parentNode.insertBefore(source,target);
}

function allclear(state) {
	var nodes=engineslist.childNodes;
	var len=nodes.length;
	for(var i=0; i<len-1; ++i) {
		engineslist.removeChild(nodes[0]);
	}
	if(state){
		for(var eg in EnginesList) {
			createIMG(eg);
		}
	}
}

function save_all() {
	var nodes=engineslist.childNodes;
	var newEgs=new Array();
	var len=nodes.length;
	for(var i=0; i<len-1; ++i) {
		newEgs.push(nodes[i].id);
	}
	localStorage.setItem("searchEngines",newEgs);
	zoomLevel=zoom.value;
	localStorage.setItem("zoomLevel",zoomLevel);
	safari.self.tab.dispatchMessage("update_setting", {Engines:newEgs, zoomLevel:zoomLevel});
}

function reset_all() {
	elist.initList();
	safari.self.tab.dispatchMessage("request_init_args", "");
}

var openTabOptions = {
	leftMost: 0,
	rightMost:1,
	rightTab: 2,
	leftTab:  3
};

var activeTabOptions = {
	background:0,
	foreground:1
};

var zoomLevel=localStorage.getItem("zoomLevel");
var tabPos=localStorage.getItem("tabPos");
var activeTab=localStorage.getItem("activeTab");
    
var EnginesList={
	google: "Google",
	baidu: "Baidu",
	bing: "Bing",
	yahoo: "Yahoo!",
	sogou: "Sogou",
	emule: "VeryCD",
	taobao: "TaoBao",
	youku: "Youku",
	tudou: "Tudou",
	iciba: "iCiba",
	dictcn: "dict.cn",
	wiki: "Wikipedia",
	macupdate: "MacUpdate",
	vtrack: "VersionTracker",
	douban: "Douban",
	about: "About.com",
	amazon: "Amazon",
	ask:   "Ask.com",
	cnet:	"CNet.com",
	ebay:	"EBay",
	espn:	"ESPN Sports",
	fb:	"FaceBook",
	imdb:	"IMDB movies",
	itunes:	"iTunes songs",
	mtv:	"MTV.com"
//	youtube: "YouTube"
};

